package com.example.deom.controller;

import com.example.deom.common.AjaxResult;
import com.example.deom.common.StringTools;
import com.example.deom.common.UserSessionTools;
import com.example.deom.entity.ArticleInfo;
import com.example.deom.entity.UserInfo;
import com.example.deom.entity.vo.ArticleInfoVO;
import com.example.deom.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request){
        //1.非空效验
        if (articleInfo == null||
           !StringUtils.hasLength(articleInfo.getTitle())||
                !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1,"参数异常");
        //2.组装数据,得到uid
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        int result = articleService.add(articleInfo);
        return AjaxResult.success(result);

    }

    @RequestMapping("/getdetailbyid")
    public AjaxResult getDetailByIdAndUid(Integer id,HttpServletRequest request){
        if(id == null || id <= 0)
            return AjaxResult.fail(-1,"非法参数");
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        return AjaxResult.success(articleService.getDetailByIdAndUid(id,userInfo.getId()));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo,HttpServletRequest request){
        //1.非空效验
        if(articleInfo == null || articleInfo.getId() <= 0||
        !StringUtils.hasLength(articleInfo.getTitle())||
        !StringUtils.hasLength(articleInfo.getContent()))
            return AjaxResult.fail(-1,"参数有误!");
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleInfo));
    }

    @RequestMapping("/getdetail")
    public AjaxResult getDetail(Integer id){
        if(id == null || id <= 0) return AjaxResult.fail(-1,"参数有误");
        ArticleInfoVO articleInfoVO = articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }

    @RequestMapping("/addrcount")
    public AjaxResult addRCount(Integer id){
        if(id == null || id <= 0)
            return AjaxResult.fail(-1,"参数有误!");
        int result = articleService.addRCount(id);
        return AjaxResult.success(result);
    }
    @RequestMapping("/mylist")
    public AjaxResult myList(HttpServletRequest request){
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        List<ArticleInfo> list = articleService.getListByUid(userInfo.getId());

        //将文章正文获正文截取成文章摘要
        for (ArticleInfo item:list) {
            String content = StringTools.subLength(item.getContent(),150);
            item.setContent(content);
        }
        return AjaxResult.success(list);

    }
    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request){
        if(id == null || id <= 0){
            return AjaxResult.fail(-1,"参数错误!");
        }
        UserInfo userInfo = UserSessionTools.getLoginUser(request);
        int result = articleService.del(id,userInfo.getId());
        return AjaxResult.success(result);
    }
    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize,Integer pageIndex){
        //默认值处理
        if(pageSize == null || pageSize <=0){
            pageSize = 2;
        }
        if(pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        //分页公式
        int  offset = (pageIndex - 1)*pageSize;
        List<ArticleInfo> list = articleService.getListByPage(pageSize,offset);
        //多线程并发循环
        list.stream().parallel().forEach(item -> {
            item.setContent(StringTools.subLength(item.getContent(),150));
        });
        return AjaxResult.success((list));
    }
    @RequestMapping("/getcount")
    public AjaxResult getCount(){
        return AjaxResult.success(articleService.getCount());
    }
}
